import { _decorator, Component, Label, labelAssembler, Node, ProgressBar } from 'cc';
import { everyDayTaskTypeEnum } from '../Enum';
import { UserInfo } from './UserInfo';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { UILayerMgr } from './UILayerMgr';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('EverydayTaskItem')
export class EverydayTaskItem extends Component {
  
    @property(Label)
    nameLabel;
    @property(ProgressBar)
    progressBar:ProgressBar;
    @property(Label)
    rewardLabel;
    @property(Node)
    isAlreadyCompleteNode:Node


    data;

    init(_data) {
        this.data = _data;
        // {
        //     "everyTaskId": 201,
        //     "everyTaskType":1,
        //     "name": "击败150只僵尸",
        //     "value":150,
        //     "rewardType":0,
        //     "rewardValue":10
        //   },
        this.rewardLabel.string = _data.rewardValue;
        this.nameLabel.string = _data.name;

        switch (parseInt(_data.everyTaskType)) {
            case everyDayTaskTypeEnum.qianghuacishu:
                this.progressBar.node.getChildByName("Label").getComponent(Label).string = `${UserInfo.todayEnhanceEquipNum}/${_data.value}`
                this.progressBar.progress = UserInfo.todayEnhanceEquipNum / parseInt(_data.value);
                break;
            case everyDayTaskTypeEnum.jibaijiangshi:
                this.progressBar.node.getChildByName("Label").getComponent(Label).string = `${UserInfo.todayBeatEnemyNum}/${_data.value}`
                this.progressBar.progress = UserInfo.todayBeatEnemyNum / parseInt(_data.value);
                break;
            case everyDayTaskTypeEnum.dakaibaoxiang:
                this.progressBar.node.getChildByName("Label").getComponent(Label).string = `${UserInfo.todayOpenBoxNum}/${_data.value}`
                this.progressBar.progress = UserInfo.todayOpenBoxNum / parseInt(_data.value);
                break;
            case everyDayTaskTypeEnum.tongguanguanqia:
                this.progressBar.node.getChildByName("Label").getComponent(Label).string = `${UserInfo.todayWinGateNum}/${_data.value}`
                this.progressBar.progress = UserInfo.todayWinGateNum / parseInt(_data.value);
                break;
        }

        if (_data.isAleadyComplete==true) {
            this.isAlreadyCompleteNode.active=true;
            this.progressBar.node.active=false;
        }
        else{
            this.isAlreadyCompleteNode.active=false;
            this.progressBar.node.active=true;
        }

    }

    onclickThisCom(){
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        if (this.data.isAleadyComplete==false) {
            if (this.progressBar.progress>=1) {//满足
                UserInfo.crystalMun+=this.data.rewardValue;
                StartSceneGameMgr.ins.createWidowTip("任务完成，获得晶体："+this.data.rewardValue);
                UserInfo.todayEveryDayTask.forEach(element => {
                    if (element.everyTaskId==this.data.everyTaskId) {
                        element.isAleadyComplete=true;
                    }
                });
                StartSceneGameMgr.ins.saveGameDate();
                UILayerMgr.getIns.resfreshUI("everyDayTaskPanel")
            }
            else{
                StartSceneGameMgr.ins.createWidowTip("未满足条件");
            }
      
        }
    }
  
}


